﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
    public float hp;//血量
    public float maxHp = 100f;//满血状态下的血量
    private Slider hpBar;//血条组件
    public GameObject fireEffectObj;//火焰特效
    public GameObject rePlacedeadObj;//残骸物体
    void Awake()
    {
        hp=maxHp;//初始化血条数值
        if(hpBar==null)
            hpBar=GameObject.Find("Canvas/LifeBar").GetComponentInChildren<Slider>();
        hpBar.maxValue=maxHp;//血条最大范围值
        hpBar.value=hp;//当前血量
        fireEffectObj.SetActive(false);
    }
    void TakeDamage(float damage)
    {
        if(hp>damage)
        {
            hp-=damage;
            if(hp<maxHp/2)
                fireEffectObj.SetActive(true);
            hpBar.value = hp;//更新血条
        }
        else
        {
            hp=0;
            hpBar.value=hp;
            if(rePlacedeadObj!=null)
                Instantiate(rePlacedeadObj,transform.position,transform.rotation);
                //实例化报废
            //销毁自己
            Destroy(gameObject);
        }
    }
}
